package com.ubao.ui.togglebutton;



import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ubao.R;
import com.ubao.util.log.Logger;




public class ToggleButton extends LinearLayout {

	private ImageView toggle;

	private boolean isChecked;
	
	public ToggleButton(Context context) {
		super(context);
		init(context);
	}

	public ToggleButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		Resources resource = getResources();
//		this.setMeasuredDimension(RelativeLayout.LayoutParams.FILL_PARENT,
//				(int) resource.getDimension(R.dimen.titlebar_height));
		this.setGravity(Gravity.CENTER_VERTICAL);
		this.setWeightSum(1);
 
		// Inflate the view from the layout resource.
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li = (LayoutInflater) context
				.getSystemService(infService);
		li.inflate(R.layout.widge_togglebutton, this);
		InitViews();
	}

	private void InitViews() {
		toggle = (ImageView) findViewById(R.id.ivToggle);
		isChecked = true;
		toggle.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(v.getId()==R.id.ivToggle){
					if(isChecked){
						isChecked = false;
						toggle.setImageResource(R.drawable.off);
					}else{
						isChecked = true;
						toggle.setImageResource(R.drawable.on);
					}
				}
			}
		});
	}
	
	public boolean isChecked(){
		return isChecked;
	}
	
	public void setChecked(boolean checked){
		if(checked){
			isChecked = true;
			toggle.setImageResource(R.drawable.on);
		}else{
			isChecked = false;
			toggle.setImageResource(R.drawable.off);
		}
	}
	
	

}
